//
//  MYPublishAddTagVC.m
//  MuYing
//
//  Created by tbb on 2019/8/14.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYPublishAddTagVC.h"
#import "YQNavTitleView.h"
#import "YQSearchBaseBar.h"
#import "MYPublishAddTagModel.h"
#import "YQImagePickerBaseVC.h"
#import "JJImagePicker.h"
#import "QMUIDialogViewController.h"
@interface MYPublishAddTagVC ()<YQSearchBaseBarDelegate>
@property (nonatomic, strong) YQSearchBaseBar * searchBar;

@property (nonatomic, strong) UIButton * cancelBtn;
@property (nonatomic, strong) MYPublishAddTagModel * tagModel;
@end

@implementation MYPublishAddTagVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.hidesBackButton = true;
}

-(void)setupView {
    [super setupView];
    YQNavTitleView *navView = [YQNavTitleView new];
    navView.frame = CGRectMake(0, 0, kScreenWidth, 44);
    navView.intrinsicContentSize = CGSizeMake(kScreenWidth, 44);
    self.navigationItem.titleView = navView;
    
    UIView *bgView = [UIView new];
    //    bgView.backgroundColor = UIColor.yellowColor;
    [navView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 44));
    }];
    
    [bgView addSubview:self.searchBar];
    
    [bgView addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.searchBar.mas_centerY).mas_equalTo(0);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(20);
    }];
    
    kYQREGISTERTABLEVIEWNIBCELL(@"MYPublishAddTagCell")
}
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
    [self.view addGestureRecognizer:pan];
//    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
//    statusBar.alpha = 1.0f; //隐藏
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count + 1;;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MYPublishAddTagCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYPublishAddTagCell"];
    cell.tagIcon.userInteractionEnabled = NO;
    if (indexPath.section == 0) {
        cell.tagIcon.userInteractionEnabled = YES;
        [cell.tagIcon addTarget:self action:@selector(addTagIcon:) forControlEvents:(UIControlEventTouchUpInside)];
        cell.tagTitleLb.text = [NSString stringWithFormat:@"%@：Nike",kYQLS(@"例如")];
        cell.subLb.text = kYQLS(@"点击创建标签");
        [cell.tagIcon setImage:YQIMG(@"Publish_AddTag_icon") forState:(UIControlStateNormal)];
    }else{
        MYPublishAddTagModel *tagModel = nil;
        if (indexPath.section - 1 < self.dataArr.count) {
            tagModel = self.dataArr[indexPath.section - 1];
        }
        cell.tagTitleLb.text = tagModel.title;
        cell.subLb.text = tagModel.sub;
        [cell.tagIcon setImage:tagModel.tagImage forState:(UIControlStateNormal)];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        [self addtagTextAction];
    }else{
        MYPublishAddTagModel *tagModel = nil;
        if (indexPath.section - 1 < self.dataArr.count) {
            tagModel = self.dataArr[indexPath.section - 1];
        }
        if (self.tagSelectBlock) {
            self.tagSelectBlock(tagModel);
        }
        [self.navigationController popViewControllerAnimated:true];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 12;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

-(void)addtagTextAction {
    QMUIDialogTextFieldViewController *dialogViewController = [[QMUIDialogTextFieldViewController alloc] init];
    dialogViewController.title = kYQLS(@"请输入标签信息");
    ws(self);
    [dialogViewController addTextFieldWithTitle:nil configurationHandler:^(QMUILabel *titleLabel, QMUITextField *textField, CALayer *separatorLayer) {
        textField.placeholder = kYQLS(@"请输入标题");
        textField.maximumTextLength = 10;
    }];
    
    [dialogViewController addTextFieldWithTitle:nil configurationHandler:^(QMUILabel *titleLabel, QMUITextField *textField, CALayer *separatorLayer) {
        textField.placeholder = kYQLS(@"请输入幅标题");
        textField.maximumTextLength = 10;
    }];
    
    dialogViewController.shouldManageTextFieldsReturnEventAutomatically = YES;// 让键盘的 Return 键也能触发确定按钮的事件。这个属性默认就是 YES，这里为写出来只是为了演示
    [dialogViewController addCancelButtonWithText:kYQLS(@"取消") block:nil];
    [dialogViewController addSubmitButtonWithText:kYQLS(@"确定") block:^(QMUIDialogViewController *dialogViewController) {
//        [QMUITips showSucceed:@"提交成功" inView:self.view hideAfterDelay:1.2];
        QMUIDialogTextFieldViewController *textFieldViewController =  (QMUIDialogTextFieldViewController *)dialogViewController;
        QMUITextField *titleField = textFieldViewController.textFields[0];
        QMUITextField *subField = textFieldViewController.textFields[1];
        [weakself addUpdateTagIcon:nil title:titleField.text sub:subField.text];
        [dialogViewController hide];
    }];
    [dialogViewController show];
//    self.currentTextFieldDialogViewController = dialogViewController;
}

-(void)addTagIcon:(UIButton *)sender {

    QMUIAlertController *sheet = [[QMUIAlertController alloc] initWithTitle:@"" message:@"" preferredStyle:(QMUIAlertControllerStyleActionSheet)];
    sheet.sheetCancelButtonAttributes = @{NSForegroundColorAttributeName:HexColor(0x999999),NSFontAttributeName:UIFontBoldMake(14),NSKernAttributeName:@(0)};
    sheet.sheetButtonAttributes = @{NSForegroundColorAttributeName:kAPPThemeColor,NSFontAttributeName:UIFontBoldMake(14),NSKernAttributeName:@(0)};
    ws(self)
    [sheet addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"从相册选取") style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        // 从相册选择
        
        JJImagePicker *picker = [JJImagePicker sharedInstance];
        [picker showImagePickerWithType:JJImagePickerTypePhoto InViewController:self didFinished:^(JJImagePicker *picker, UIImage *image) {
            
            [weakself addUpdateTagIcon:image title:nil sub:nil];
            
        }];
    }]];
    
    [sheet addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"拍照") style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        
        // 拍照
        JJImagePicker *picker = [JJImagePicker sharedInstance];
        picker.customCropViewController = ^TOCropViewController *(UIImage *image) {
            if (picker.type == JJImagePickerTypePhoto) {
                //使用默认
                return nil;
            }
            TOCropViewController  *cropController = [[TOCropViewController alloc] initWithImage:image];
            //选择框可以按比例来手动调节
            cropController.aspectRatioLockEnabled = NO;
            //设置选择宽比例
            cropController.aspectRatioPreset = TOCropViewControllerAspectRatioPresetSquare;
            //显示选择框比例的按钮
            cropController.aspectRatioPickerButtonHidden = NO;
            //显示选择按钮
            cropController.rotateButtonsHidden = NO;
            //设置选择框可以手动移动
            cropController.cropView.cropBoxResizeEnabled = YES;
            return cropController;
        };
        [picker showImagePickerWithType:JJImagePickerTypeCamera InViewController:self didFinished:^(JJImagePicker *picker, UIImage *image) {
            [weakself addUpdateTagIcon:image title:nil sub:nil];
        }];
        
    }]];
    
    [sheet addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"取消") style:QMUIAlertActionStyleCancel handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        
    }]];
    
    [sheet showWithAnimated:YES];
    
}
-(void)addUpdateTagIcon:(UIImage *)image title:(NSString *)title sub:(NSString *)sub {
     MYPublishAddTagCell *tagCell = (MYPublishAddTagCell *)[self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    if (image) {
        self.tagModel.tagImage = image;
        [tagCell.tagIcon setImage:self.tagModel.tagImage forState:(UIControlStateNormal)];
    }
    
    if (title && sub) {
        self.tagModel.title = title;
        self.tagModel.sub = sub;
        tagCell.tagTitleLb.text = title;
        tagCell.subLb.text = sub;
    }
    //self.tagModel.tagImage != nil && 
    if (self.tagModel.title != nil && self.tagModel.sub != nil) {
        [self.dataArr addObject:self.tagModel];
        self.tagModel = nil;
        [self.listTableView reloadData];
    }
    

}
#pragma makr -- lazy
-(YQSearchBaseBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[YQSearchBaseBar alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH - 68, 28)];
        _searchBar.top = 11;
        _searchBar.delegate = self;
        _searchBar.placeholderStr = kYQLS(@"搜索您想要的标签");
    }
    return _searchBar;
}

-(UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:0];
        _cancelBtn.titleLabel.font = kSystemMedium(14);
        [_cancelBtn setTitleColor:HexColor(0xFA969D) forState:(UIControlStateNormal)];
        [_cancelBtn setTitle:kYQLS(@"取消") forState:(UIControlStateNormal)];
        ws(self);
        [_cancelBtn addTapAction:^(id  _Nonnull sender) {
            [weakself.navigationController popViewControllerAnimated:true];
        }];
    }
    return _cancelBtn;
}
-(MYPublishAddTagModel *)tagModel {
    if (!_tagModel) {
        _tagModel = [[MYPublishAddTagModel alloc] init];
    }
    return _tagModel;
}
#pragma makr -- lazy end

#pragma makr -- YQSearchBaseBarDelegate
- (void)yq_searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    
}
-(void)yq_textFieldShouldClear:(UITextField *)textField {
    
}
-(void)yq_shouldChangeText:(NSString *)text {
    
}
#pragma makr -- YQSearchBaseBarDelegate end
@end
